﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmEditInitConsumer : BaseForm
    {
        public string ID = string.Empty;
        public string RoomType = string.Empty;

        public FrmEditInitConsumer()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InitDictItem()
        {
            this.txtItemName.Items.Clear();
            string goodsType = SystemConfig.Default.KFInitConsumerGoodsType;
            List<GoodsInfo> goodList = BLLFactory<Goods>.Instance.FindByGoodsType(goodsType);
            foreach (GoodsInfo info in goodList)
            {
                this.txtItemName.Items.Add(new CListItem(info.Name, info.ID.ToString()));
            }
        }

        private void FrmEditRoomType_Load(object sender, EventArgs e)
        {
            InitDictItem();

            string chkNotClose = RegistryHelper.GetValue(Constants.IS_GOODS_EDIT_OPEN);
            if (!string.IsNullOrEmpty(chkNotClose))
            {
                this.chkNotClose.Checked = (chkNotClose == "0") ? false : true;
            }

            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                InitConsumerInfo info = BLLFactory<InitConsumer>.Instance.FindByID(ID);
                if (info != null)
                {
                    this.txtNote.Text = info.Note;
                    this.txtItemName.Text = info.ItemName;
                    this.txtPrice.Text = info.Price.ToString("f2");
                    this.txtDiscountRate.Text = info.Discount.ToString("f2");
                    this.txtQuantity.Text = info.Quantity.ToString("f2");
                    this.txtAmount.Text = info.Amount.ToString("f2");
                    this.txtDiscountPrice.Text = info.DiscountPrice.ToString("f2");
                    //this.RoomType = info.RoomType;
                }
            }
            else
            {
                this.Text = "新建 " + this.Text;
            }
        }

        private void SetInfo(InitConsumerInfo info)
        {
            CListItem item = this.txtItemName.SelectedItem as CListItem;
            GoodsInfo goodsInfo = BLLFactory<Goods>.Instance.FindByID(item.Value);
            info.ItemType = goodsInfo.GoodsType;
            info.ItemName = goodsInfo.Name;
            info.ItemNo = goodsInfo.ItemNo;

            info.Price = Convert.ToDecimal(this.txtPrice.Text);
            info.Note = this.txtNote.Text;
            info.Quantity = Convert.ToDouble(this.txtQuantity.Text);
            info.RoomType = RoomType;
            info.Amount = Convert.ToDecimal(this.txtAmount.Text);
            info.Discount = Convert.ToDouble(this.txtDiscountRate.Text);
            info.DiscountPrice = Convert.ToDecimal(this.txtDiscountPrice.Text);
        }

        private void ClearInput()
        {
            this.ID = "";
            this.txtNote.Text = "";
            
            this.Text = this.Text.Replace("编辑", "新建");
            this.txtItemName.Focus();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 检查
            if (this.txtItemName.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("请选择项目名称");
                this.txtItemName.Focus();
                return;
            }

            #endregion

            if (!string.IsNullOrEmpty(ID))
            {
                InitConsumerInfo info = BLLFactory<InitConsumer>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<InitConsumer>.Instance.Update(info, info.ID.ToString());
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            if (this.chkNotClose.Checked)
                            {
                                ClearInput();
                            }
                            else
                            {
                                this.DialogResult = DialogResult.OK;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                InitConsumerInfo info = new InitConsumerInfo();
                SetInfo(info);

                try
                {
                    bool succeed = BLLFactory<InitConsumer>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        if (this.chkNotClose.Checked)
                        {
                            ClearInput();
                        }
                        else
                        {
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void chkNotClose_CheckedChanged(object sender, EventArgs e)
        {
            //保存注册表，记录下来
            RegistryHelper.SaveValue(Constants.IS_GOODS_EDIT_OPEN, this.chkNotClose.Checked ? "1" : "0");
        }

        private void txtItemName_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.txtItemName.SelectedItem as CListItem;
            if (item != null)
            {
                GoodsInfo info = BLLFactory<Goods>.Instance.FindByID(Convert.ToInt32(item.Value));
                if (info != null)
                {
                    this.txtPrice.Text = info.Price.ToString("f2");
                }
            }
        }

        private void txtDiscountPrice_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtDiscountPrice.Text) && ValidateUtil.IsNumeric(txtDiscountPrice.Text))
            {
                decimal oldPrice = Convert.ToDecimal(this.txtPrice.Text);
                decimal discountPrice = Convert.ToDecimal(this.txtDiscountPrice.Text);
                if (oldPrice > 0 && discountPrice > 0)
                {
                    txtDiscountRate.Text = (discountPrice / oldPrice).ToString("f2");
                }
                else
                {
                    txtDiscountRate.Text = "0";
                }
            }
        }

        private void txtDiscountRate_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtDiscountRate.Text) && ValidateUtil.IsNumeric(txtDiscountRate.Text))
            {
                decimal oldPrice = Convert.ToDecimal(this.txtPrice.Text);
                decimal discountRate = Convert.ToDecimal(this.txtDiscountRate.Text);
                if (oldPrice > 0 && discountRate > 0)
                {
                    txtDiscountPrice.Text = (discountRate * oldPrice).ToString("f2");
                }
                else
                {
                    txtDiscountPrice.Text = "0";
                }
            }
        }

        private void txtQuantity_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtQuantity.Text) && ValidateUtil.IsNumeric(txtQuantity.Text))
            {
                decimal discountPrice = Convert.ToDecimal(this.txtDiscountPrice.Text);
                this.txtAmount.Text = (discountPrice * Convert.ToDecimal(this.txtQuantity.Text)).ToString();
            }
        }
    }
}
